#include <string.h>

#include <algorithm>
#include <memory>
#include <string>

class Solution {
 public:
  bool backspaceCompare(std::string s, std::string t)
  {
    int s_slow = 0;
    int t_slow = 0;

    auto f = [](std::string& s, int& slow) -> void {
      for (int i = 0; i != s.size(); ++i)
      {
        if (s[i] == '#')
        {
          if (slow > 0) { --slow; }
        } else
        {
          s[slow] = s[i];
          ++slow;
        }
      }
    };

    f(s, s_slow);
    f(t, t_slow);

    return std::equal(s.begin(), s.begin() + s_slow, t.begin(),
                      t.begin() + t_slow);
  }
};
